package com.sks.core.tag;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class FenYeTag extends SimpleTagSupport {
	private String actionPath;
   private String method = "post";
   private String totalRows;
   private int pageSize = 20;
   private int curPageNo = 1;
   private int totalPages = 0;
   private boolean isNewForm = true;
   private String formName;
   private Map<String, String> hiddenList;

   public String getMethod() {
      return method;
   }

   public void setMethod(String method) {
      this.method = method;
   }

   public String getActionPath() {
      return actionPath;
   }

   public void setActionPath(String actionPath) {
      this.actionPath = actionPath;
   }

   public String getTotalRows() {
      return totalRows;
   }

   public void setTotalRows(String totalRows) {
      this.totalRows = totalRows;
   }

   public int getPageSize() {
      return pageSize;
   }

   public void setPageSize(int pageSize) {
      this.pageSize = pageSize;
   }

   public int getCurPageNo() {
      return curPageNo;
   }

   public void setCurPageNo(int curPageNo) {
      this.curPageNo = curPageNo;
   }

   public Map<String, String> getHiddenList() {
      return hiddenList;
   }

   public void setHiddenList(Map<String, String> hiddenList) {
      this.hiddenList = hiddenList;
   }

   public int getTotalPages() {
      return totalPages;
   }

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public boolean getIsNewForm() {
		return isNewForm;
	}

	public void setIsNewForm(boolean isNewForm) {
		this.isNewForm = isNewForm;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public void doTag() throws JspException, IOException { 
		
	  JspWriter outer = this.getJspContext().getOut();
      if (this.totalPages != 0 && Integer.parseInt(totalRows) % this.pageSize == 0) {
         this.totalPages = Integer.parseInt(this.totalRows) / this.pageSize;
      } else {
         if (this.totalRows != "") {
            this.totalPages = Integer.parseInt(this.totalRows) / this.pageSize + 1;
         }
      }
      
      if (isNewForm) {
    	  formName = "formPages";
    	  outer.write("<form name=\"" + formName + "\" method=\""+method+"\" action=\""+actionPath+"\">\n");
      }
	   
	   outer.write("<input type=\"hidden\" name=\"pageSize\" value=\""+pageSize+"\"/>\n");
       outer.write("<input type=\"hidden\" name=\"page\" value=\""+curPageNo+"\"/>\n");
       outer.write("<input type=\"hidden\" name=\"totalRows\" value=\""+totalRows+"\"/>\n");
       outer.write("<input type=\"hidden\" name=\"totalPages\" value=\""+totalPages+"\"/>\n");
	   outer.write("<div class=\"multi\">\n");
	   //outer.write(" <div class=\"left_count\">共有<strong>" + totalRows + "</strong>条记录</div>\n");
	   outer.write(" <div class=\"right_page\">\n");
	   outer.write("    <div class=\"multipage\">\n");
	   if (curPageNo == 1 || curPageNo == 0) {
	      outer.write("       <span class=\"prexpage nolink all_icon\">首页</span>\n");
	      outer.write("       <span class=\"prexpage nolink all_icon\">上一页</span>\n");
	   } else {
	      outer.write("       <a href=\"javascript:page_first();\"><span class=\"prexpage all_icon\">首页</span></a>\n");
	      outer.write("       <a href=\"javascript:page_pre();\"><span class=\"prexpage all_icon\">上一页</span></a>\n");
	   }
	   
	   int index = 1;
	   if (curPageNo - 6 > 0) {
	      index = curPageNo - 5;
	   }
	   
	   for (int i = index ; i <= index + 10 && i <= totalPages ; i++) {
	      if (i == curPageNo) {
	         outer.write("<span class=\"current\">" + i + "</span>\n");
	      } else {
	         outer.write("<a href=\"javascript:page_go(" + i + ");\"><span>" + i + "</span></a>\n");
	      }
	   }

		if (curPageNo == totalPages) {
			outer.write("       <span class=\"nextpage nolink all_icon\">下一页</span>\n");
			outer.write("       <span class=\"nextpage nolink all_icon\">末页</span>\n");
		} else {
			outer.write("       <a href=\"javascript:page_next();\"><span class=\"nextpage all_icon\">下一页</span></a>\n");
			outer.write("       <a href=\"javascript:page_last();\"><span class=\"nextpage all_icon\">末页</span></a>\n");
		}
		outer.write("    </div>\n");
		outer.write(" </div>\n");
		outer.write(" <div class=\"clear\"></div>\n");
		outer.write("</div>\n");

      if ((hiddenList != null)) {
         Set<String> set = hiddenList.keySet();
         String temp1, temp2;
         int i = 0;
         java.util.Iterator<String> iterator = set.iterator();
         while (i < set.size()) {
            temp1 = iterator.next();
            temp2 = this.hiddenList.get(temp1);
            if ((temp1 == null) || (temp2 == null) || (temp1 == "")
                  || (temp1 == "")) {
               i++;
               continue;
            }
            outer.write("<input type=\"hidden\" name=\"" + temp1
                  + "\" value=\"" + temp2 + "\"/>\n");
            i++;
         }
      }
      if (isNewForm) {
    	  outer.write("</form>\n");
      }
      writeScript();
	}
	public void writeScript()throws JspException, IOException {
	   JspWriter outer = this.getJspContext().getOut();
		outer.write("<script>\n");
		outer.write("	function page_go(pageNoValue) {\n");
		outer.write("		document." + formName + ".page.value = pageNoValue;\n");
		outer.write("		page_validate();\n");
		outer.write("		document." + formName + ".submit();\n");
		outer.write("	}\n");
		outer.write("	function page_first() {\n");
		outer.write("		document." + formName + ".page.value = 1;\n");
		outer.write("		page_validate();\n");
		outer.write("		document." + formName + ".submit();\n");
		outer.write("	}\n");
		outer.write("	function page_pre() {\n");
		outer.write("		var pageNo = document." + formName + ".page.value;\n");
		outer.write("		if (parseInt(pageNo)-1<1) pageNo=1;\n");
		outer.write("		else pageNo = parseInt(pageNo)-1;\n");
		outer.write("		document." + formName + ".page.value = pageNo;\n");
		outer.write("		page_validate();\n");
		outer.write("		document." + formName + ".submit();\n");
		outer.write("	}\n");
		outer.write("	function page_next() {\n");
		outer.write("		var pageNo = document." + formName + ".page.value;\n");
		outer.write("		var pageTotal = document." + formName + ".totalPages.value;\n");
		outer.write("		if (parseInt(pageNo)+1>pageTotal) pageNo = pageTotal;\n");
		outer.write("		else pageNo = parseInt(pageNo) + 1;\n");
		outer.write("		document." + formName + ".page.value = pageNo;\n");
		outer.write("		page_validate();\n");
		outer.write("		document." + formName + ".submit();\n");
		outer.write("	}\n");
		outer.write("	function page_last() {\n");
		outer.write("		document." + formName + ".page.value = document." + formName + ".totalPages.value;\n");
		outer.write("		page_validate();\n");
		outer.write("		document." + formName + ".submit();\n");
		outer.write("	}\n");
		outer.write("	function page_validate() {\n");
		outer.write("		var pageTotal = document." + formName + ".totalPages.value;\n");
		outer.write("		var pageNo = document." + formName + ".page.value;\n");
		outer.write("		if (parseInt(pageNo) < 1) pageNo = 1;\n");
		outer.write("		if (parseInt(pageNo) > parseInt(pageTotal)) {\n");
		outer.write("			pageNo = pageTotal;\n");
		outer.write("		}\n");
		outer.write("		document." + formName + ".page.value = pageNo;\n");
		outer.write("		var size = document." + formName + ".pageSize.value;\n");
		outer.write("		if (parseInt(size) < 1) size = 1;\n");
		outer.write("		document." + formName + ".pageSize.value = size;\n");
		outer.write("		var pageSize = parseInt(document." + formName + ".pageSize.value);\n");
		outer.write("		var totalRows = parseInt(document." + formName + ".totalRows.value);\n");
		outer.write("		if(totalRows%pageSize == 0) { \n");
		outer.write("         document." + formName + ".totalPages.value = totalRows/pageSize;\n");
		outer.write("       } else {\n");
		outer.write("         document." + formName + ".totalPages.value = parseInt(totalRows/pageSize) + 1;\n");
		outer.write("		}\n");
		outer.write("	}\n");
		outer.write("</script>\n");
	}
}